glibc.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # The following comment should be removed at some point in the future.
  2. # mypy: strict-optional=False
  3. from __future__ import absolute_import
  4. import os
  5. import sys
  6. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  7. if MYPY_CHECK_RUNNING:
  8. from typing import Optional, Tuple
  9. def glibc_version_string():
  10. # type: () -> Optional[str]
  11. "Returns glibc version string, or None if not using glibc."
  12. return glibc_version_string_confstr() or glibc_version_string_ctypes()
  13. def glibc_version_string_confstr():
  14. # type: () -> Optional[str]
  15. "Primary implementation of glibc_version_string using os.confstr."
  16. # os.confstr is quite a bit faster than ctypes.DLL. It's also less likely
  17. # to be broken or missing. This strategy is used in the standard library
  18. # platform module:
  19. # https://github.com/python/cpython/blob/fcf1d003bf4f0100c9d0921ff3d70e1127ca1b71/Lib/platform.py#L175-L183
  20. if sys.platform == "win32":
  21. return None
  22. try:
  23. # os.confstr("CS_GNU_LIBC_VERSION") returns a string like "glibc 2.17":
  24. _, version = os.confstr("CS_GNU_LIBC_VERSION").split()
  25. except (AttributeError, OSError, ValueError):
  26. # os.confstr() or CS_GNU_LIBC_VERSION not available (or a bad value)...
  27. return None
  28. return version
  29. def glibc_version_string_ctypes():
  30. # type: () -> Optional[str]
  31. "Fallback implementation of glibc_version_string using ctypes."
  32. try:
  33. import ctypes
  34. except ImportError:
  35. return None
  36. # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
  37. # manpage says, "If filename is NULL, then the returned handle is for the
  38. # main program". This way we can let the linker do the work to figure out
  39. # which libc our process is actually using.
  40. process_namespace = ctypes.CDLL(None)
  41. try:
  42. gnu_get_libc_version = process_namespace.gnu_get_libc_version
  43. except AttributeError:
  44. # Symbol doesn't exist -> therefore, we are not linked to
  45. # glibc.
  46. return None
  47. # Call gnu_get_libc_version, which returns a string like "2.5"
  48. gnu_get_libc_version.restype = ctypes.c_char_p
  49. version_str = gnu_get_libc_version()
  50. # py2 / py3 compatibility:
  51. if not isinstance(version_str, str):
  52. version_str = version_str.decode("ascii")
  53. return version_str
  54. # platform.libc_ver regularly returns completely nonsensical glibc
  55. # versions. E.g. on my computer, platform says:
  56. #
  57. # ~$ python2.7 -c 'import platform; print(platform.libc_ver())'
  58. # ('glibc', '2.7')
  59. # ~$ python3.5 -c 'import platform; print(platform.libc_ver())'
  60. # ('glibc', '2.9')
  61. #
  62. # But the truth is:
  63. #
  64. # ~$ ldd --version
  65. # ldd (Debian GLIBC 2.22-11) 2.22
  66. #
  67. # This is unfortunate, because it means that the linehaul data on libc
  68. # versions that was generated by pip 8.1.2 and earlier is useless and
  69. # misleading. Solution: instead of using platform, use our code that actually
  70. # works.
  71. def libc_ver():
  72. # type: () -> Tuple[str, str]
  73. """Try to determine the glibc version
  74. Returns a tuple of strings (lib, version) which default to empty strings
  75. in case the lookup fails.
  76. """
  77. glibc_version = glibc_version_string()
  78. if glibc_version is None:
  79. return ("", "")
  80. else:
  81. return ("glibc", glibc_version)